package com.jilian.mydemo.modul.my.ui;

import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jilian.mydemo.Constant;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.factory.Factoty;
import com.jilian.mydemo.modul.user.activity.LoginActivity;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.SPUtil;
import com.jilian.mydemo.utils.ToastUitl;
import com.jilian.mydemo.utils.Utils;
import com.jilian.mydemo.views.ClearEditText;

public class UpdatePwdActivity extends BaseActivity {

    private ClearEditText etPwdOle;
    private RelativeLayout rlEyeOne;
    private ImageView ivEyeOne;
    private ClearEditText etPwd;
    private RelativeLayout rlEyeTwo;
    private ImageView ivEyeTwo;
    private ClearEditText etPwdOk;
    private RelativeLayout rlEyeThree;
    private ImageView ivEyeThree;
    private TextView tvOk;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_updatepwd;
    }

    @Override
    public void initView() {
        setNormalTitle("密码管理", v -> finish());
        etPwdOle = (ClearEditText) findViewById(R.id.et_pwd_ole);
        rlEyeOne = (RelativeLayout) findViewById(R.id.rl_eye_one);
        ivEyeOne = (ImageView) findViewById(R.id.iv_eye_one);
        etPwd = (ClearEditText) findViewById(R.id.et_pwd);
        rlEyeTwo = (RelativeLayout) findViewById(R.id.rl_eye_two);
        ivEyeTwo = (ImageView) findViewById(R.id.iv_eye_two);
        etPwdOk = (ClearEditText) findViewById(R.id.et_pwd_ok);
        rlEyeThree = (RelativeLayout) findViewById(R.id.rl_eye_three);
        ivEyeThree = (ImageView) findViewById(R.id.iv_eye_three);
        tvOk = (TextView) findViewById(R.id.tv_ok);

    }

    @Override
    public void initData() {

    }
    private boolean onebDisplayFlg;
    private boolean twobDisplayFlg;
    private boolean threebDisplayFlg;
    @Override
    public void initListener() {
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updatePwd();
            }
        });
        rlEyeOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!onebDisplayFlg) {
                    // display password text, for example "123456"
                    etPwdOle.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivEyeOne.setImageResource(R.drawable.image_login_open);
                } else {
                    // hide password, display "."
                    etPwdOle.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivEyeOne.setImageResource(R.drawable.image_login_close);
                }
                etPwdOle.setSelection(etPwdOle.getText().toString().length());//将光标移至文字末尾
                onebDisplayFlg = !onebDisplayFlg;
                etPwdOle.postInvalidate();

            }
        });
        rlEyeTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!twobDisplayFlg) {
                    // display password text, for example "123456"
                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivEyeTwo.setImageResource(R.drawable.image_login_open);
                } else {
                    // hide password, display "."
                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivEyeTwo.setImageResource(R.drawable.image_login_close);
                }
                etPwd.setSelection(etPwd.getText().toString().length());//将光标移至文字末尾
                twobDisplayFlg = !twobDisplayFlg;
                etPwd.postInvalidate();

            }
        });
        rlEyeThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!threebDisplayFlg) {
                    // display password text, for example "123456"
                    etPwdOk.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivEyeThree.setImageResource(R.drawable.image_login_open);
                } else {
                    // hide password, display "."
                    etPwdOk.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivEyeThree.setImageResource(R.drawable.image_login_close);
                }
                etPwdOk.setSelection(etPwdOk.getText().toString().length());//将光标移至文字末尾
                threebDisplayFlg = !threebDisplayFlg;
                etPwdOk.postInvalidate();

            }
        });
        etPwdOk.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                updateEnableStatus();
            }
        });
        etPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                updateEnableStatus();
            }
        });
        etPwdOle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                updateEnableStatus();
            }
        });
    }

    private void updatePwd() {
        if(etPwd.getText().toString().equals(etPwdOle.getText().toString())){
            ToastUitl.showImageToastTips("新旧密码不能一致"); return;
        }
        if(!etPwd.getText().toString().equals(etPwdOk.getText().toString())){
          ToastUitl.showImageToastTips("两次输入密码不一致"); return;
        }
        if(!Utils.isLetterDigit(etPwd.getText().toString())){
            ToastUitl.showImageToastTips(getResources().getString(R.string.error_pwd));
            return;
        }
        showLoadingDialog();
        Factoty.getViewModel(UserViewModel.class,UpdatePwdActivity.this).updateUserPassword(etPwdOle.getText().toString(),etPwdOk.getText().toString());
        Factoty.getViewModel(UserViewModel.class,UpdatePwdActivity.this).getUpdatePwdliveData().observe(this, new Observer<BaseDto>() {
            @Override
            public void onChanged(@Nullable BaseDto dto) {
                hideLoadingDialog();
                if(dto.isSuccess()){
                    finish();
                    ToastUitl.showImageToastSuccess("修改成功");
                    SPUtil.clearData(Constant.SP_VALUE.SP);
                    MyApplication.getInstance().clearAllActivitys();
                    Intent intent = new Intent(UpdatePwdActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                else{
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });
    }

    /**
     * 更新按钮的可用状态
     */
    private void updateEnableStatus() {
        if (!TextUtils.isEmpty(etPwdOle.getText().toString())
                && !TextUtils.isEmpty(etPwd.getText().toString())
                && !TextUtils.isEmpty(etPwdOk.getText().toString())) {
            tvOk.setEnabled(true);
            tvOk.setBackgroundResource(R.drawable.shape_btn_login_normal);
        } else {
            tvOk.setEnabled(false);
            tvOk.setBackgroundResource(R.drawable.shape_btn_login_dark);
        }
    }
}
